<template>
	<view class="pages">
		<!-- 收货地址 -->
		<view v-if="addressList.length > 0">
			<view class="addressContent" v-for="(item,index) in addressList" :key="index" @tap.stop="changeAddress(item,index)">
				<!-- 地址信息 -->
				<view class="addressInfo">
					<view class="leftInfo">
						<view class="info">
							<view class="name">{{item.fullName}} {{item.mobile}}</view>
							<view class="address">{{item.provinceText}}{{item.cityText}}{{item.areaText}}{{item.address}}</view>
						</view>
					</view>
				</view>
				<!-- 地址设置 -->
				<view class="addressSet">
					<radio-group @tap.stop="chooseType(item,index)" >
						 <label>
						   <radio class="radio" :value="item.index" color="rgba(83, 167, 0, 1)" :checked="item.isDefault == 1?true:false" /><span>默认寄件地址</span>
						 </label>
					</radio-group>
					
					<view style='display: flex;align-items: center;'>
						<view style="margin-right: 40rpx;" @tap.stop="handleEditAddress(item)">
							<image src="../../static/mine/edit.png" class="editImg" ></image>
							编辑
						</view>
						
						<view style="margin-left: 12rpx;" @tap.stop="handleDelAddress(item,index)">
							<image src="../../static/mine/del.png" class="editImg" ></image>
							删除
						</view>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 暂无收货地址 -->
		<view class="noneContent" v-else>
			<image src="../../static/index/noneData.png" class="addressImg"></image>
			<view class="text">您还没有收获地址</view>
		</view>
		<!-- 新建地址 -->
		<view class="addAddress" @tap="goAddAddress">添加收货地址</view>
	</view>
</template>

<script>
	
	import { addressList,editAddress,delAddress } from '../../api/api.js'
	export default {
		data() {
			return {
				// 用户id
				userId:'',
				// 地址列表
				addressList:[],
				isSubmit:false,   // 是否是从提交订单页面进入
			}
		},
		onLoad(e) {
			this.isSubmit = e.isSubmit == 1?true:false
		},
		onShow() {
			this.addressList = []
			// 获取收货地址列表
			this.getAddressList()
		},
		methods:{
			// 获取收货地址列表
			getAddressList() {
				let data = {}
				addressList(data).then( res=> {
					this.addressList = res.data
					
				})
			},
			// 编辑地址
			handleEditAddress(item) {
				
				let addressInfo = JSON.stringify(item)
				uni.navigateTo({
					url:'./addAddress?item=' + addressInfo + '&isEdit=' + 1
				})
			},
			// 删除收货地址
			handleDelAddress(item,index) {
				var that = this
				let data = {
					id:item.id
				}
				delAddress(data).then( res=> {
					uni.showToast({
						title:'删除成功',
						icon:'none'
					})
					this.addressList.splice(index, 1)
					
				})
			},
			// 选择默认地址
			chooseType(item,index) {
				let data = {
					fullName:item.fullName,
					mobile:item.mobile,
					provinceText:item.provinceText,
					cityText:item.cityText,
					areaText:item.areaText,
					address:item.address,
					isDefault:item.isDefault==1?0:1,
					id:item.id,
				}
				editAddress(data).then( res=> {
					uni.showToast({
						title:'设置成功',
						icon:'none'
					})
					// 获取收货地址列表
					this.getAddressList()
					
				})
			},
			// 选择地址
			changeAddress(item,index) {
				if( !this.isSubmit ) {
					return
				}
				let data = {
					fullName:item.fullName,
					mobile:item.mobile,
					provinceText:item.provinceText,
					cityText:item.cityText,
					areaText:item.areaText,
					address:item.address,
					isDefault:1,
					id:item.id,
				}
				editAddress(data).then( res=> {
					// uni.redirectTo({
					// 	url:'/pages/goods/submitOrder'
					// })
					uni.navigateBack({
						delta:1
					})
				})
			},
			// 前往新建地址
			goAddAddress() {
				uni.navigateTo({
					url:'./addAddress'
				})
			}
		}
	}
</script>

<style lang="less">
	.pages {
		letter-spacing: 1rpx;
		box-shadow: inset 0px 4px 8px 0rpx rgba(0, 0, 0, 0.05);
		// 收货地址
		.addressContent {
			border-bottom: #FAFAFA 10rpx solid;
			padding-top: 42rpx;
			// 地址信息
			.addressInfo {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 30rpx;
				width: 690rpx;
				padding-bottom: 14rpx;
				border-bottom: 2rpx solid rgba(221, 218, 218, 0.25);
				.leftInfo {
					display: flex;
					align-items: flex-start;
					
					.home {
						width: 52rpx;
						height: 32rpx;
						background: linear-gradient(134deg, rgba(255, 116, 98, 0.99) 0%, #D71F36 100%);
						border-radius: 4rpx;
						text-align: center;
						line-height: 32rpx;
						font-size: 22rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: rgba(255,255,255);
					}
					.company {
						text-align: center;
						line-height: 32rpx;
						width: 60rpx;
						height: 32rpx;
						background: linear-gradient(145deg, #60A5F5 0%, #53A0FD 0%, #4D97ED 49%, #4A90E2 100%, #4A90E2 100%);
						border-radius: 4rpx;
						font-size: 22rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
					}
					.school {
						text-align: center;
						line-height: 32rpx;
						width: 52rpx;
						height: 32rpx;
						background: linear-gradient(137deg, #FFC069 0%, #FF794F 100%);
						border-radius: 4rpx;
						font-size: 22rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
					}
					.info {
						margin-left: 16rpx;
						.name {
							margin-bottom: 20rpx;
							font-family: DengXian, DengXian;
							font-weight: bold;
							font-size: 28rpx;
							color: #3C3C3C;
							line-height: 32rpx;
						}
						.address {
							font-family: DengXian, DengXian;
							font-weight: 400;
							font-size: 28rpx;
							color: #6E6E6E;
							line-height: 48rpx;
						}
					}
				}
				.editImg {
					width: 32rpx;
					height: 32rpx;
				}
			}
		  // 地址设置
			.addressSet {
				padding: 23rpx 0 30rpx 0;
				margin-left: 30rpx;
				width: 690rpx;
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #000000;
				display: flex;
				align-items: center;
				justify-content: space-between;
				radio .wx-radio-input {
					width: 30rpx;
					height: 30rpx;
					border: #808080 solid;
				}
				radio .wx-radio-input.wx-radio-input-checked {
				  border-color: #F7533E;
				  background:linear-gradient(134deg, rgba(255, 116, 98, 0.99) 0%, #D71F36 100%);
				}
				
				font-family: Alibaba PuHuiTi, Alibaba PuHuiTi;
				font-weight: 400;
				font-size: 28rpx;
				color: #767676;
				line-height: 28rpx;
				
				.editImg {
					width: 22rpx;
					height: 22rpx;
				} 
				
				// 删除
				.del {
					width: 120rpx;
					height: 52rpx;
					border-radius: 26rpx;
					border: 2rpx solid #FE3B33;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #FE3B33;
					text-align: center;
					line-height:52rpx;
				}
			}
		}
		.noneContent {
			display: flex;
			flex-direction: column;
			align-items: center;
			.addressImg {
				margin-top: 162rpx;
				width: 276rpx;
				height: 146rpx;
			}
			.text {
				margin: 20rpx 0 326rpx 0;
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #808080;
			}
		}
		// 新建地址
		.addAddress {
			width: 670rpx;
			text-align: center;
			line-height: 88rpx;
			height: 88rpx;
			background: #00C538;
			border-radius: 88rpx;
			color: #FAFAFA;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #FFFFFF;
			margin: 60rpx 0 0 40rpx;
		}
	}
</style>
